package main

// 优雅的通知子gorutine退出 利用全局变量
import (
	"fmt"
	"sync"
	"time"
)

func f() {
	var n int = 0
	for {
		n++
		fmt.Println("## ", n)
		if notify {
			break
		}
		time.Sleep(1 * time.Second)
	}
	// defer wg.Done()
}

var wg sync.WaitGroup
var notify bool

func main() {
	wg.Add(1)
	go f()
	time.Sleep(5 * time.Second)
	notify = true
	wg.Wait() //阻塞

}
